import logging

import pytest

from resource.pom.camera import Camera
from resource.pom.gallery import Gallery
from utils.manager import Direction, Images, MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：删除图库图片------')
    device.mediatool_delete_all()
    yield
    logging.info('------用例后置：删除图库图片------')
    device.mediatool_delete_all()


class Test:
    app = Camera

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入桌面-相机应用，选择拍照模式')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1：拍照模式显示正常')
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        device.assert_text_exist('录像')
        logging.info('步骤2：点击拍照按钮')
        # 左滑切到拍照模式
        device.dirc_fling(Direction.Left)
        device.click(*self.app.shot_button)
        device.wait(2)
        logging.info('预期结果2：左下角生成缩略图')
        thumbnail_pic = device.save_snapshot_to_local('thumbnail.jpeg')
        Images.crop_picture(thumbnail_pic, *self.app.thumbnail_area)
        similarity = Images.compare_image_similarity(thumbnail_pic, self.app.standard_thumbnail_pic)
        assert similarity < 0.95, '缩略图未生成'

        logging.info('步骤3：点击左下角缩略图跳转，查看图片')
        device.click(*self.app.thumbnail)
        device.wait(2)
        logging.info('预期结果3：能正常拍照并保存到相册, 点击缩略图正常跳转，图片滑动和展示正常')
        device.assert_current_window(Gallery.window_name)
        device.refresh_layout()
        device.assert_text_exist('收藏')
        device.assert_text_exist('删除')
        device.assert_key_exist('browserFocus_file', MatchRules.Contain)